import React from 'react';
import {
  Form,
  Input,
  Button,
  Table,
  Icon,
  Radio,
  InputNumber,
  Checkbox,
  Modal,
  message,
  notification,
  Affix,
  Upload,
  Select
} from 'antd';

import InnerTable from 'components/DBTable/InnerTable.js';
import ajax from 'utils/ajax';

const FormItem = Form.Item;
const ButtonGroup = Button.Group;
const Option = Select.Option;

var _this;
let flag = 1,switchflag = 1;

/**
 * 内部表格组件
 */
class WeexVersionInnerTable extends InnerTable {


  /**
   * 构造器，好像是用来初始化的？
   * @param props
   */
  constructor(props) {
    super(props);
    this.state.moduleName = 'WeexVersion';
    this.state = {
      ...this.state,
      upOpt: ['OPTION_MD5'],
    }
    this.responseStatus = 1;
  }



  /**
   * 这里好像是用来配置那些按钮之类的
   * @returns {Array}
   */
  prepareToolBar = function () {

    let isButtondisabled = false;

    if (this.state.selectedRowKeys.length > 0) {  // 是否选择
      isButtondisabled = true;
    }

    if (this.state.selectedRowKeys.length > 1) {  // 是否选择了多项
      isButtondisabled = false;
    }

    var items = [];

    items.push(<Button key='look' onClick={this.onClickView} disabled={!isButtondisabled} ><Icon type="info-circle-o" />查看</Button>);

    if(isButtondisabled){
      if(this.state.selectedRows[0] && this.state.selectedRows[0].status && this.state.selectedRows[0].status == 1) {//如果是上线状态
        items.push(<Button key='offLineBtn' disabled={!isButtondisabled} onClick={this.onClickToChangeState}>
          <Icon type="edit"/>下线
        </Button>);
      }else {
        items.push(<Button key='onlineBtn' disabled={!isButtondisabled} onClick={this.onClickToChangeState}>
          <Icon type="edit"/>上线
        </Button>);
      }
    }else{
      items.push(<Button disabled={false} key='upDown' >
        <Icon type="edit"/>选择单个上下线
      </Button>);
    }

    return items;

  }


  /**
   * 点击查看
   * @param e
   */
  onClickView = (e) =>{
    // console.log("点击查看啦");
    e.preventDefault();
    this.resetFields();
    // console.log("this.state 里面的数据是", this.state);
    let selectedRowsPop = this.state.selectedRows[0];
    
    // console.log("selectedRowsPop 里面的数据是", selectedRowsPop);

    
    // console.log("this.props.form 里面的数据是", this.props.form.getFieldsValue);
    if(selectedRowsPop){
      let defaultFileObj = [];
      let defaultFileItem = {};
      defaultFileItem.uid = selectedRowsPop.id;
      defaultFileItem.name = "点击下载文件";
      defaultFileItem.status = "done";
      defaultFileItem.url = selectedRowsPop.patchUrl;
      defaultFileObj.push(defaultFileItem);

      this.setState({fileObjs : defaultFileObj});
    }

    console.log("zheg",selectedRowsPop)
    var state={ modalVisible: true, modalTitle: '查看', modalState: this.modalActionState.View,modalForm:true};

    console.log(selectedRowsPop);
    this.setState(state,()=>{
      this.props.look(selectedRowsPop);
    });
  }

  /**
   * 处理表格数据
   * @param obj
   * @param newObj
   */
  preDealTableData=(obj,newObj)=>{


  }

  /**
   * 点击上下线
   * @param e
   */
  onClickToChangeState = (e) =>{

    Modal.confirm({
      title:  '确认' + (this.state.selectedRows[0].status == 1 ? '下线' : '上线'),
      // content: `${this.state.selectedRowKeys.join(', ')}`,确认要进行上线/下线操作吗？
      content: "确认要进行上线/下线操作吗？",
      // 这里注意要用箭头函数, 否则this不生效
      onOk: () => {
        this.handleChangeStatus(this.state.selectedRows[0].status == 1 ? 2 : 1);
      },
    });
  }

  /**
   * 真正去更新状态
   */
  handleChangeStatus = (status ) =>{

    _this = this;
    ajax.post({
      url:"/weexversion/changeStatus",
      params:{id:this.state.selectedRowKeys.join(""),status:status},
      success:function(res){
        if(res.errorCode==200){
          _this.props.refresh(); //刷新列表
          message.success('修改成功');
        }
      }
    })

  }


  /**
   * 点击新增之前处理数据
   */
  handleInsertBefore = () =>{
    delete this.state.fileObjs; //删除u上传框那里的文件
  }

  
  /**
   * 上传文件  调用的方法
   * @param info
   */
  onFileUpChange = (info)=>{
    let fileList = info.fileList;
     if (fileList.length > 1) {
        fileList = fileList.slice(-1); //最多只能有一个文件
      }
    const name = fileList[0].name;
     if(name.substr(name.indexOf('.')+1) !== 'zip'){
      notification.error({
        message: "文件上传格式不对",
        description: "请重新上传~~~"
      });
    }else{
      if (info.file.response) {
        fileList = fileList.map((file) => {
          file.url = file.response.data.url;
          return file;
        });
        let fileKey = info.file.response.data.key;
        let md5Code = info.file.response.data.md5Code;
        this.props.form.setFieldsValue({fileKey:fileKey});
        this.props.form.setFieldsValue({md5Code:md5Code});
      }
      
      
      const nameSmall = name.substr(0,name.indexOf('.'))
      this.houZhui(name.substr(name.indexOf('.')+1))
      this.setState({ fileObjs: fileList },()=>{
        
      }); 
      this.props.form.validateFields((err,value)=>{
        if(!err){
          if(value.appName !== nameSmall && flag){
            notification.error({
              message: "图片上传名称和所输入的名称不一样",
              description: "请修改~~~"
            });
            flag = 0;
          }  
        }
      })
    }
  }

  /**
   * 调用固定选择的select时候的回掉
   * @param info
   */
  onSelectByDefaultValueChange = (info) =>{
    console.log(info);
  }


  handleData = (data) =>{

    let fileObjs = this.state.fileObjs;
    console.log("fileList里面的数据是", fileObjs);

    if(data.actionState != 3 && (!fileObjs || fileObjs[0].status != "done")){
      notification.error({
        message: "图片上传中",
        description: "请等待~~~"
      });
      return null;
    }


    let iosNativeVersionCodeArray = data.params.iosNativeVersionCodeStr;
    let androidNativeVersionCodeArray = data.params.androidNativeVersionCodeStr;

    let iosNativeVersionCodeStr = "";
    if(iosNativeVersionCodeArray){
      for(let i=0; i<iosNativeVersionCodeArray.length; i++){
        iosNativeVersionCodeStr += (iosNativeVersionCodeArray[i] + ",");
      }
    }
    iosNativeVersionCodeStr = iosNativeVersionCodeStr.substring(0, iosNativeVersionCodeStr.length-1);
    data.params.iosNativeVersionCodeStr = iosNativeVersionCodeStr;


    let androidNativeVersionCodeStr = "";
    if(androidNativeVersionCodeArray){
      for(let i=0; i<androidNativeVersionCodeArray.length; i++){
        androidNativeVersionCodeStr += (androidNativeVersionCodeArray[i] + ",");
      }
    }
    androidNativeVersionCodeStr = androidNativeVersionCodeStr.substring(0, androidNativeVersionCodeStr.length-1);

    data.params.androidNativeVersionCodeStr = androidNativeVersionCodeStr;

    return data;
  }
}

export default WeexVersionInnerTable;
